package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysPointpot;
import com.ruoyi.system.domain.SysPoints;
import com.ruoyi.system.domain.SysRegset;
import com.ruoyi.system.mapper.SysPointpotMapper;
import com.ruoyi.system.mapper.SysPointsMapper;
import com.ruoyi.system.mapper.SysRegsetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysMemberMapper;
import com.ruoyi.common.core.domain.entity.SysMember;
import com.ruoyi.system.service.ISysMemberService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-27
 */
@Service
public class SysMemberServiceImpl implements ISysMemberService 
{
    @Autowired
    private SysMemberMapper sysMemberMapper;
    @Autowired
    private SysRegsetMapper sysRegsetMapper;
    @Autowired
    private SysPointsMapper sysPointsMapper;
    @Autowired
    SysPointpotMapper sysPointpotMapper;
    @Resource
    private TransactionTemplate transactionTemplate;
    /**
     * 查询【请填写功能名称】
     * 
     * @param userid 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysMember selectSysMemberByUserid(Long userid)
    {
        return sysMemberMapper.selectSysMemberByUserid(userid);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param sysMember 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysMember> selectSysMemberList(SysMember sysMember)
    {
        return sysMemberMapper.selectSysMemberList(sysMember);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param sysMember 【请填写功能名称】
     * @return 结果
     */
    @Override
    public boolean insertSysMember(SysMember sysMember)
    {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime sevenDaysLater = now.plusDays(7);
        Boolean execute = transactionTemplate.execute(e -> {
            List<SysRegset> regsetList = sysRegsetMapper.selectSysRegsetList(new SysRegset());
             sysMember.setGroupid(regsetList.get(0).getGroupid());
             sysMember.setVgroupid(regsetList.get(0).getVgroupid());
             if(regsetList.get(0).getGroupid()>0&&regsetList.get(0).getVgroupid()>0)
                 sysMember.setVipdate(DateUtils.toDate(LocalDateTime.now().plusDays(regsetList.get(0).getGroupid())));
             else
                 sysMember.setVipdate(null);
             SysPoints sysPoints = sysPointsMapper.selectSysPointsById(1l);
             sysMemberMapper.insertSysMember(sysMember);
             SysPointpot sysPointpot = new SysPointpot();
             sysPointpot.setIp(sysMember.getRegip());
             sysPointpot.setUserid(sysMember.getUserid());
             sysPointpot.setType(1l);
             sysPointpot.setPoints(sysPoints.getPoints());
             sysPointpot.setDocid(0l);
             sysPointpotMapper.insertSysPointpot(sysPointpot);

            return Boolean.TRUE;
        });
        return  execute;
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param sysMember 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysMember(SysMember sysMember)
    {
        return sysMemberMapper.updateSysMember(sysMember);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param userids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberByUserids(String userids)
    {
        return sysMemberMapper.deleteSysMemberByUserids(Convert.toStrArray(userids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param userid 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberByUserid(Long userid)
    {
        return sysMemberMapper.deleteSysMemberByUserid(userid);
    }
    /**
     * 通过用户名查询用户
     *
     * @param userName 用户名
     * @return 用户对象信息
     */
    @Override
    public SysMember selectUserByLoginName(String userName)
    {
        return sysMemberMapper.selectUserByLoginName(userName);
    }
    /**
     * 校验手机号码是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public boolean checkPhoneUnique(String data)
    {
        SysMember info = sysMemberMapper.checkPhoneUnique(data);
        if ((StringUtils.isNotNull(info) && data.equals(info.getPhone())) || (StringUtils.isNotNull(info) && data.equals(info.getUsername()))) // 增加email
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    @Override
    public boolean checkPhoneUnique_1(SysMember sysMember)
    {
        Long userId = StringUtils.isNull(sysMember.getUserid()) ? -1L : sysMember.getUserid();
        SysMember info = sysMemberMapper.checkPhoneUnique(sysMember.getUsername());

        if (StringUtils.isNotNull(info) && info.getUserid()!=userId)
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    /**
     *
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public boolean checkOpenidUnique(String data)
    {
        SysMember info = sysMemberMapper.checkOpenidUnique(data);
        if (StringUtils.isNotNull(info) && data.equals(info.getOpenwx()))
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    @Override
    public boolean checkInvicodeUnique(String data)
    {
        SysMember info = sysMemberMapper.checkInvicodeUnique(data);
        if (StringUtils.isNotNull(info) && data.equals(info.getIinvicode()))
        {
            return UserConstants.UNIQUE;
        }
        return UserConstants.NOT_UNIQUE;
    }
    @Override
    public int updateGlod(Long userid, BigDecimal increase)
    {
        return sysMemberMapper.updateGlod(userid,increase);
    }
    public int updatePoint(Long userid, Long increase)
    {
        return sysMemberMapper.updatePoint(userid,increase);
    }

}
